package com.hospital.insurancepeople.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.insurancepeople.module.entity.InsuranceLogs;
import com.hospital.insurancepeople.module.mapper.InpSettleMasterMapper;
import com.hospital.insurancepeople.module.mapper.InsuranceLogsMapper;
import com.hospital.insurancepeople.utils.SpringContextHolder;

import java.util.List;
import java.util.Objects;

/**
 * @Author: lvyao
 * @Date: 2023-04-03-14:51
 * @Description:医保结算清单转发(包括所有在app_url_info表中配置peo_url_value的消息转发)
 */
public class Minsur4101 extends AbstractMessageInfo {
    private InsuranceLogsMapper insuranceLogsMapper = SpringContextHolder.getBean(InsuranceLogsMapper.class);
    private InpSettleMasterMapper inpSettleMasterMapper = SpringContextHolder.getBean(InpSettleMasterMapper.class);

    @Override
    Tip getUpDetail(JSONObject requestVal) {
        Tip tip = new SuccessTip();
        String uploadDate = requestVal.getString("UPLOAD_DATE");
        String serialNo = requestVal.getString("SERIAL_NO");
        String flag = requestVal.getString("FLAG");
        //通过配置地址查看是否转发
        String url = insuranceLogsMapper.selectUrl(serialNo
                + "_URL");
        Object input = null;
        if (!Objects.isNull(url) && flag.equals("1")) {
            //因表中无主键，因此根据查询条件查出几条就推送
            QueryWrapper<InsuranceLogs> queryWrapper = new QueryWrapper<>();
            queryWrapper.apply(" SERIAL_NO = {0} " +
                    "and UPLOAD_DATE =to_date({1},'yyyy-mm-dd hh24:mi:ss')", serialNo, uploadDate);
            List<InsuranceLogs> insuranceLogsList = insuranceLogsMapper.
                    selectList(queryWrapper);
            if (!Objects.isNull(insuranceLogsList)) {
                InsuranceLogs insuranceLogs = insuranceLogsList.get(0);
                String insuranceJson = insuranceLogs.getInsuranceJson();
                Object parse = JSON.parse(insuranceJson);
                JSONObject jsonObjectInput = JSON.parseObject(parse.toString());
                String infno = jsonObjectInput.getString("infno");
                input = jsonObjectInput.get("input");
                if (infno.equals("2401")) {
                    String msgid = jsonObjectInput.getString("msgid");
                    String mdtrtId = insuranceLogsMapper.getMdtrtId(msgid);
                    JSONObject jsonObject2 = JSON.parseObject(jsonObjectInput.get("input").toString());
                    Object mdtrtinfo = jsonObject2.get("mdtrtinfo");
                    JSONObject jsonObject3 = JSON.parseObject(mdtrtinfo.toString());
                    jsonObject3.put("mdtrt_id", mdtrtId);
                    jsonObject2.put("mdtrtinfo", jsonObject3);
                    input = jsonObject2;
                }
//                                    if (infno.equals("2302")){
//                                        Object data1 = JSON.parseObject(input.toString()).get("data");
//                                        input = data1;
//                                    }

            }
            tip.setData(input);
            tip.setMessage(url);
            tip.setCode(0);
        } else {
            tip = new ErrorTip(1002, "医保结算清单未查询到数据！");
        }
        return tip;
    }
}
